package com.hbvtc.xxxy.nikeSale.frames;

/*
 * 添加员工信息
 */
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import com.hbvtc.xxxy.nikeSale.daos.SalesmanDao;
import com.hbvtc.xxxy.nikeSale.models.Salesman;

public class AddSalesmanNike extends JFrame implements ActionListener {
	JPanel jPanel;
	JButton btnAdd;
	JLabel lalId, lalName, lalTel, lalSex, lalSalary, lalGrade, lalPwd;
	JTextField txtId, txtName, txtTel, txtSalary, txtGrade, txtPwd;
	JRadioButton jrb1, jrb2;
	SalesmanDao salesmanDao = new SalesmanDao();
	String Gender;

	public AddSalesmanNike() {
		this.setTitle("新员工登记管理");
		this.setSize(1000, 700);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		// 设置标题栏的图标
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());

		// 背景图片的路径
		String path = "images/001.jpg";
		ImageIcon background = new ImageIcon(path);
		// 把背景图片显示在一个标签里面
		JLabel label = new JLabel(background);
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		// 把背景图片添加到分层窗格的最底层作为背景
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

		this.setLayout(null);
		lalId = new JLabel("员工编号：");
		lalName = new JLabel("员工姓名：");
		lalTel = new JLabel("员工电话：");
		lalSex = new JLabel("员工性别：");
		lalSalary = new JLabel("员工薪水：");
		lalGrade = new JLabel("员工级别：");
		lalPwd = new JLabel("密       码:");
		btnAdd = new JButton("立即添加");

		jrb1 = new JRadioButton("男");
		jrb2 = new JRadioButton("女");
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(jrb1);
		buttonGroup.add(jrb2);

		txtId = new JTextField();
		txtName = new JTextField();
		txtTel = new JTextField();
		txtSalary = new JTextField();
		txtGrade = new JTextField();
		txtPwd = new JTextField();
		this.add(lalId);
		this.add(lalName);
		this.add(lalTel);
		this.add(lalSex);
		this.add(lalSalary);
		this.add(lalGrade);
		this.add(lalPwd);
		this.add(btnAdd);
		this.add(txtId);
		this.add(txtName);
		this.add(txtTel);
		this.add(txtSalary);
		this.add(txtGrade);
		this.add(txtPwd);
		this.add(jrb1);
		this.add(jrb2);

		lalId.setBounds(150, 70, 120, 30);
		lalId.setFont(new Font(null, 0, 24));
		lalName.setBounds(150, 140, 120, 30);
		lalName.setFont(new Font(null, 0, 24));
		lalTel.setBounds(150, 210, 120, 30);
		lalTel.setFont(new Font(null, 0, 24));
		lalSex.setBounds(150, 280, 120, 30);
		lalSex.setFont(new Font(null, 0, 24));
		lalSalary.setBounds(150, 350, 120, 30);
		lalSalary.setFont(new Font(null, 0, 24));
		lalGrade.setBounds(150, 420, 120, 30);
		lalGrade.setFont(new Font(null, 0, 24));
		lalPwd.setBounds(150, 490, 120, 30);
		lalPwd.setFont(new Font(null, 0, 24));

		txtId.setBounds(400, 70, 260, 30);
		txtId.setFont(new Font(null, 0, 24));
		txtName.setBounds(400, 140, 260, 30);
		txtName.setFont(new Font(null, 0, 24));
		txtTel.setBounds(400, 210, 260, 30);
		txtTel.setFont(new Font(null, 0, 24));
		jrb1.setBounds(400, 280, 50, 30);
		jrb1.setFont(new Font(null, 0, 24));
		jrb2.setBounds(500, 280, 50, 30);
		jrb2.setFont(new Font(null, 0, 24));
		txtSalary.setBounds(400, 350, 260, 30);
		txtSalary.setFont(new Font(null, 0, 24));
		txtGrade.setBounds(400, 420, 260, 30);
		txtGrade.setFont(new Font(null, 0, 24));
		txtPwd.setBounds(400, 490, 260, 30);
		txtPwd.setFont(new Font(null, 0, 24));
		
		btnAdd.setBounds(620, 550, 150, 40);
		btnAdd.setFont(new Font(null, 0, 24));

		jrb1.addActionListener(this);
		jrb2.addActionListener(this);
		btnAdd.addActionListener(this);
	}

	public static void main(String[] args) {
		new AddSalesmanNike().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == jrb1)
			Gender = "男";
		if (e.getSource() == jrb1)
			Gender = "女";
		if (e.getSource() == btnAdd) {
			Salesman salesman = new Salesman();
			int id = Integer.parseInt(txtId.getText().trim());
			String name = txtName.getText();
			String mobile = txtTel.getText();
			String price = txtSalary.getText();
			String role = txtGrade.getText();
			String pwd = txtPwd.getText();
			try {
				salesman.setSalesmanId(id);
				salesman.setSalesmanName(name);
				salesman.setMobile(mobile);
				salesman.setGender(Gender);
				salesman.setBaseSalary(price);
				salesman.setRole(role);
				salesman.setPwd(pwd);
				salesmanDao = new SalesmanDao();
				try {
					if (salesmanDao.getBySalesmanId(id) == true) {
						salesmanDao.AddSalesman(salesman);
						JOptionPane.showMessageDialog(AddSalesmanNike.this,
								"正式员工注册成功");
					} else {
						JOptionPane.showMessageDialog(AddSalesmanNike.this,
								"注册失败");
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			} catch (NumberFormatException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
	}
}
